<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/11/28
 * Time: 11:47 AM
 */
namespace App\Model;
use App\Model\BaseModel;
use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\Utility\SnowFlake;
class HistoryMsgModel extends BaseModel
{
    protected $table = 'bjyadmin_histroymsg';
    function getAll($condition = [], int $page = 1, $pageSize = 10,$special=0,$orderBy='id',$order='DESC'): array
    {
      
        $allow = ['where', 'orWhere', 'join', 'orderBy', 'groupBy'];
        foreach ($condition as $k => $v) {
            if (in_array($k, $allow)) {
                foreach ($v as $item) {
                    $this->getDb()->$k(...$item);
                }
            }
        }
        $list = $this->getDb()
            ->withTotalCount()
            ->orderBy($orderBy, $order)
            ->get($this->table, [$pageSize * ($page - 1), $pageSize]);
        $total = $this->getDb()->getTotalCount();
        return ['total' => $total, 'list' => $list];
    }
    function getOne($userid)
    {
        $user = $this->getDb()
            ->where('userid', $userid,'=')
            ->getOne($this->table);
        if (empty($user)) {
            return null;
        }
        return $user;
    }
    function update($userid, array $data)
    {
        $this->getDb()->where('userid', $userid)->update($this->table, $data);
        return $this->getDb()->getAffectRows();
    }
    function register(array $data)
    {
        return $this->getDb()->insert($this->table, $data);
    }
    function delete($userid)
    {
        return $this->getDb()->where('userid', $userid)->delete($this->table);
    }
    //统计未读消息数
    function countUnreadMsg($groupId,$messageId){
        return $this->getDb()->where('toUserId', $groupId,'=')->where('messageId',$messageId,'>')->withTotalCount()->get($this->table);
    }
}